package com.ayz.gateway.assist.application;


import com.ayz.gateway.assist.config.GatewayServiceProperties;
import com.ayz.gateway.core.context.context.ApiGatewayContext;
import com.ayz.gateway.sdk.gatawayService.GatewayServiceSdk;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;

/**
 * @ClassName GatewayContextCloseListener
 * @Description TODO: 监听器 监听到 ContextCloseEvent事件发生后 完成网关上下文的关闭
 * 主要是将 网关服务上下文 和 通信组件SDK 进行关闭
 * @Author 快乐的星球
 * @Date 2023/5/25 11:38
 * @Version 1.0
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GatewayContextCloseListener implements ApplicationListener<ContextClosedEvent> {
    private final static Logger LOG=Logger.getLogger(GatewayContextCloseListener.class);
    /**
     * 网关上下文
     */
    private ApiGatewayContext apiGatewayContext;
    /**
     * 网关服务的配置参数
     */
    private GatewayServiceProperties properties;

    /**
     * 网关服务网络通信组件
     */
    private GatewayServiceSdk gatewayServiceSdk;
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        LOG.info("网关服务组件开始关闭......");
        /**
         * 当前类需要完成以下几件事?
         * 1. 向网关注册中心 注销当前网关服务,并取消订阅
         * 2. 关闭网关服务上下文 和 通信组件
         */
        this.apiGatewayContext.shutdown();
        LOG.info("网关服务上下文关闭成功!");
        this.gatewayServiceSdk.close();
        LOG.info("网络通信组件关闭成功!");
    }
}
